// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
const tool = require('ToolUtil');
const HeadsManager = require('HeadsManager');
cc.Class({
    extends: cc.Component,

    properties: {
        _sex: 0,
        _dbID: 0,
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    // start() {

    // },


    setDefaultHead: function (sex) {
        // if (this.mIsWebImage)
        //     return;
        // if (sex == 1) {
        //     if (this.mFileType == 0) {
        //         this.mHeadSpr.initWithFile('img_defaulttx.png');
        //     } else {
        //         this.mHeadSpr.initWithSpriteFrameName('img_defaulttx.png');
        //     }
        // } else if (sex == -1) {
        //     if (this.mFileType == 0) {
        //         this.mHeadSpr.initWithFile('img_defaulttx2.png');
        //     } else {
        //         this.mHeadSpr.initWithSpriteFrameName('img_defaulttx2.png');
        //     }
        // } else {
        //     if (this.mFileType == 0) {
        //         this.mHeadSpr.initWithFile('img_defaulttx1.png');
        //     } else {
        //         this.mHeadSpr.initWithSpriteFrameName('img_defaulttx1.png');
        //     }

        // }
    },

    initWithFileUrl: function (hurl, sex, dbcardID) {
        if (sex == null)
            sex = 1;
        this.setDefaultHead(sex);
        this._sex = sex;
        if (hurl == null || hurl.length == 0) {
            return true;
        }
        this._dbID = dbcardID;


        if (hurl.lastIndexOf('/') != -1) {
            hurl = hurl.substring(0, hurl.lastIndexOf('/')) + '/64';
        }

        var headerCache = HeadsManager.getHeadTextureByID(dbcardID);
        if (headerCache !== undefined) {
            var spr = this.getComponent(cc.Sprite);
            if (spr) {
                spr.spriteFrame = headerCache;
            }
        } else {
            tool.Notification.on('headLoad' + dbcardID, function (texture) {
                if (!cc.isValid(this))
                    return;
                if (texture) {
                    var spr = this.getComponent(cc.Sprite);
                    if (spr) {
                        spr.spriteFrame = texture;
                    }
                } else {
                    this.setDefaultHead(this._sex);
                }
                tool.Notification.off('headLoad' + this._dbID, this);
            }.bind(this), this);
            //判断是否已经在加载当前的图片了。如果已经在加载了。则不用再加载,否则进行加载
            if (HeadsManager.getLoadMap().indexOf(dbcardID) != -1)
                return;
            HeadsManager.toLoadHeadTexture(dbcardID, hurl);
        }
    }

    // update (dt) {},
});